package com.eightbears.beboy.ui.merchant.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.RefuntInfoAdapter;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;
import com.eightbears.beboy.entity.ReserveEntity;
import com.eightbears.beboy.entity.status.Status_Error;

import org.greenrobot.eventbus.EventBus;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_SUBMIT_BOOKS;
import static com.eightbears.beboy.ui.merchant.activity.ShopReservesActivity.PARAMS_RESERVE_INFO;

public class ShopRefuseActivity extends BaseActivity {

    public static final String EVENT_REFUSE = "refuse";

    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.et_text)
    EditText et_text;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;

    private RefuntInfoAdapter mPaymentAdapter;
    private ReserveEntity.ReserveInfo mReserveInfo;
    private int mPosition;
    private String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_refuse);

        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tv_title.setText("拒绝预约");
        initAdapter();
    }

    @Override
    protected void initData() {
        mReserveInfo = (ReserveEntity.ReserveInfo) getIntent().getExtras().get(PARAMS_RESERVE_INFO);
    }

    @Override
    protected void initEvent() {
        mPaymentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mPaymentAdapter.checked(position);
                mPosition = position;
                if (position == 1) {
                    et_text.setVisibility(View.VISIBLE);
                } else {
                    et_text.setVisibility(View.GONE);
                }
            }
        });
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.tv_refunt_btn)
    void refuse() {
        if(validInput()){
            executeReserve();
        }
    }

    @OnClick(R.id.ll_back) void back(){
        finish();
    }
    /**
     * 预订确认
     */
    private void executeReserve() {
        String url = URL_SUBMIT_BOOKS + "/" + mReserveInfo.getId();
        OkHttpUtils.put().url(url)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("status", "2")
                .addParams("comment", text)
                .build().execute(new StringCallback() {
            @Override
            public void onResponse(String response) {
                EventBus.getDefault().post(EVENT_REFUSE);
                finish();
            }

            @Override
            public void onError(Call call, Exception e) {
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(ShopRefuseActivity.this, status_error);
            }
        });
    }

    private boolean validInput(){
        if(mPosition == 0) {
            text = mPaymentAdapter.getData().get(mPosition);
            return true;
        }

        if(mPosition == 1) {
            text = et_text.getText().toString().trim();
            if(TextUtils.isEmpty(text)){
                ShowToast.showShortToast(getString(R.string.text_input_refuse));
                return false;
            }
            return true;
        }
        return false;
    }

    private void initAdapter() {
        mPaymentAdapter = new RefuntInfoAdapter();
        rv_list.setLayoutManager(new MyLinearLayoutManager(this));
        List<String> items = Arrays.asList(getResources().getStringArray(R.array.refuseList));
        mPaymentAdapter.setNewData(items);
        rv_list.setAdapter(mPaymentAdapter);
        mPaymentAdapter.checked(0);
    }
}
